package GUI;

import model.*;
import order.*;

import javax.swing.*;

import GUI.MapPanel.TerritoryPanel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


public class UpgradeTroopFrame extends OrderFrame{
	 
	 private UpgradeTroopFrame upgradeOrderFrame;
	 private JComboBox<Integer> troopAmountBox;
	 private JComboBox<TechnologyLevel> sourceBox;
	 private JComboBox<TechnologyLevel> destinationBox;
	 private UpgradeTroop temp;

	 public UpgradeTroopFrame(TextPanel textPanel,  TerritoryPanel territoryPanel, Map map){
		super(textPanel, territoryPanel, map);
		upgradeOrderFrame = this;
		
		this.setLayout(new GridLayout(4, 2, 10, 10));	   
		
	  	this.add(new JLabel("Upgrade troops from: "));
	  	sourceBox = new JComboBox<TechnologyLevel>();
	  	sourceBox.addItem(null);
	  	for (TechnologyLevel level: TechnologyLevel.values())
	  	   if (territory.getTroopAmount(level) > 0)
	  		   sourceBox.addItem(level);
	  	sourceBox.addActionListener(new sourceSelectedListener());
	  	this.add(sourceBox);
	  	troopAmountBox = new JComboBox<Integer>();
	  	destinationBox = new JComboBox<TechnologyLevel>();
	  	this.add(new JLabel("To: "));
	  	this.add(destinationBox);
	  	this.add(new JLabel("Amount: "));
	  	this.add(troopAmountBox);
	  	
	  	this.add(submitButton);
	  	this.add(cancelButton);
	  	submitButton.addActionListener(new submitListener());
	  	cancelButton.addActionListener(new cancelListener(this));
	  	
	  	setSize(400, 200);
	  	setVisible(true);
	 }
	 
     class sourceSelectedListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			JComboBox cb = (JComboBox)e.getSource();
			TechnologyLevel Source = (TechnologyLevel)cb.getSelectedItem();
      			troopAmountBox.removeAllItems();
      			destinationBox.removeAllItems();
      			troopAmountBox.addItem(0);
      		  	for (int i = 1; i <= territory.getTroopAmount(Source); i++)
      		  		troopAmountBox.addItem(i);
      		  for (TechnologyLevel level : TechnologyLevel.values())
      	  		if ((level.CombatBonus() > Source.CombatBonus()) && (level.CombatBonus() <= territory.getOwner().getLevel().CombatBonus()))
      	  		  destinationBox.addItem(level);
		}
    	 
     }
	 class submitListener implements ActionListener{
   
	  public void actionPerformed(ActionEvent arg0) {
		TechnologyLevel source = (TechnologyLevel)sourceBox.getSelectedItem();
		TechnologyLevel destination = (TechnologyLevel)destinationBox.getSelectedItem();
		 int amount = (int)troopAmountBox.getSelectedItem();
	  	 temp = new UpgradeTroop(territory, player, source, destination, amount);
	  	 if (temp.checkOrder()){
		  	    textPanel.addHistory(temp);
		  	    temp.executeOrder();
		  	  textPanel.updatePlayerPanel();
		  	territoryPanel.update();
		     }
	  	 upgradeOrderFrame.dispose();
	}
	 }
 
}

